# author: hulingchuan@hotmail.com
#
# VacBuilder
# build the vocabulary

require 'nokogiri'
require 'open-uri'
require_relative './logger_singleton'

class VacBuilder
  URL  = 'http://www.manythings.org/vocabulary/lists/c/'
  BASE = '<ul>
<li><a href="words.php?f=3esl.01">Words Beginning with A</a></li>
<li><a href="words.php?f=3esl.02">Words Beginning with B</a></li>
<li><a href="words.php?f=3esl.03">Words Beginning with C</a></li>
<li><a href="words.php?f=3esl.04">Words Beginning with D</a></li>
<li><a href="words.php?f=3esl.05">Words Beginning with E</a></li>
<li><a href="words.php?f=3esl.06">Words Beginning with F</a></li>
<li><a href="words.php?f=3esl.07">Words Beginning with G</a></li>
<li><a href="words.php?f=3esl.08">Words Beginning with H</a></li>
<li><a href="words.php?f=3esl.09">Words Beginning with I</a></li>
<li><a href="words.php?f=3esl.10">Words Beginning with J</a></li>
<li><a href="words.php?f=3esl.11">Words Beginning with K</a></li>
<li><a href="words.php?f=3esl.12">Words Beginning with L</a></li>
<li><a href="words.php?f=3esl.13">Words Beginning with M</a></li>
<li><a href="words.php?f=3esl.14">Words Beginning with N</a></li>
<li><a href="words.php?f=3esl.15">Words Beginning with O</a></li>
<li><a href="words.php?f=3esl.16">Words Beginning with P</a></li>
<li><a href="words.php?f=3esl.17">Words Beginning with Q</a></li>
<li><a href="words.php?f=3esl.18">Words Beginning with R</a></li>
<li><a href="words.php?f=3esl.19">Words Beginning with S</a></li>
<li><a href="words.php?f=3esl.20">Words Beginning with T</a></li>
<li><a href="words.php?f=3esl.21">Words Beginning with U</a></li>
<li><a href="words.php?f=3esl.22">Words Beginning with V</a></li>
<li><a href="words.php?f=3esl.23">Words Beginning with W</a></li>
<li><a href="words.php?f=3esl.24">Words Beginning with X, Y &amp; Z</a></li>
</ul>'

  def initialize
    @logger = LoggerSingleton.instance
    @logger.info 'initializing word builder'

    @base  = Nokogiri::HTML(BASE)
    @words = Hash.new {|h,k| h[k] = []}
    @total_count = 0
    @built = false

    @logger.info 'initialized word builder'
  end

  public
  def build

    @logger.info 'starting build word'

    @base.xpath('//ul//a').each { |line|
      sub_url = URL + line.attributes['href'].value
      sub     = nil
      while sub.nil?
        begin
          sub     = Nokogiri::HTML(open(sub_url))
        rescue
          @logger.info 'network error, retrying...'
        end
      end
      parse_sub sub
    }
    @built = true

    @logger.info "word built, #{@total_count} words in total"
  end

  def get_words_length (len)

    @logger.info 'selecting candidate words'

    return 'you should build vac first' unless @built

    @words[len].dup
  end

  def parse_sub (sub)
    sub.xpath('//li').each { |word|
      store word.children.to_s.gsub(/[^a-zA-Z]/, '')
    }
  end

  def store (word)
    len = word.to_s.length
    @words[len].push(word.upcase)
    @total_count += 1
  end

end
